package com.jimster.plugins.gadgets.scroller
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Stage;
	
	public class ScrollerContent extends MovieClip
	{
		//Variables
		private var mcContent:DisplayObject;
		private var mcScrub:MovieClip;
		private var mcScrubBar:MovieClip;
		private var mcContainer:MovieClip;
		private var mcMask:MovieClip;
		private var mcScrubContainer:MovieClip;
		private var numMaskHeight:Number;
		private var numMaskWidth:Number;
		private var numScrollerBarX:Number;
		private var numScrollerBarY:Number;
		private var rootStage:Stage;
		private var numOffSetScrubBarHeight:Number;
		private var numOffSetScrubBarWidth:Number;
		
		//Constructor
		public function ScrollerContent()
		{
			super();
		}
		
		//Public methods
		public function initialize( $data:ScrollerContentData ):void
		{
			this.mcContainer = new MovieClip();
			this.addChild( this.mcContainer );
			
			this.mcContent = $data.content;
			this.mcScrub = $data.scrub;
			this.mcScrubBar = $data.scrubBar;
			this.numMaskHeight = $data.maskHeight;
			this.numMaskWidth = $data.maskWidth;
			this.numScrollerBarX = $data.scrollerBarX;
			this.numScrollerBarY = $data.scrollerBarY;
			this.numOffSetScrubBarWidth = $data.scrollerBarOffSetWidth;
			this.numOffSetScrubBarHeight = $data.scrollerBarOffSetHeight;
			
			this.rootStage = $data.rootStage;
			
			this.addContent();
			this.addMask();
		}
		
		public function updateContent( $data:* ):void
		{
			this.mcContainer.removeChild( this.mcContent );
			this.mcContent = null;
			this.mcContent = $data;
			
			this.mcContainer.removeChild( this.mcMask );
			this.mcMask = null;
			
			this.addContent();
			this.addMask();
		}
		
		//Private methods
		private function createScroller():void
		{
			this.mcScrubContainer = new MovieClip();
			this.mcContainer.addChild( this.mcScrubContainer );
			
			this.mcScrubContainer.addChild( this.mcScrubBar );
			this.mcScrubContainer.addChild( this.mcScrub );
			
			( this.numMaskHeight == 0  ) ? this.mcScrubBar.width = ( this.numMaskWidth + this.numOffSetScrubBarWidth ) : this.mcScrubBar.height = ( this.numMaskHeight + this.numOffSetScrubBarHeight );
			
			this.mcScrubContainer.x = this.numScrollerBarX;
			this.mcScrubContainer.y = this.numScrollerBarY;
		}
		
		private function addContent():void
		{
			this.mcContent.cacheAsBitmap = true;
			this.mcContainer.addChild( this.mcContent );
		}
		
		private function addMask():void
		{
			this.mcMask = new MovieClip();
			this.mcMask.cacheAsBitmap = true;
			
			this.mcMask.graphics.beginFill( 0x00FF00 );
			( this.numMaskHeight == 0  ) ? this.mcMask.graphics.drawRect( 0, 0, this.numMaskWidth, this.mcContent.height ) : this.mcMask.graphics.drawRect( 0, 0, this.mcContent.width, this.numMaskHeight );
			this.mcMask.graphics.endFill();
			
			this.mcContainer.addChild( this.mcMask );
			
			this.mcContent.mask = this.mcMask;
			this.createScroller();
		}
		
		//Event handlers
	}
}